package com.jason.cloud.engine.share.driver.grant.controller;


import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.enterprise.bean.EnterpriseBean;
import com.jason.cloud.engine.share.driver.grant.bean.GrantBean;
import com.jason.cloud.engine.share.driver.grant.service.GrantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/grant")
public class GrantController {

    private static final Logger logger = LoggerFactory.getLogger(GrantController.class);

    @Resource
    private GrantService grantService;

    /**
     * 加载
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/query/{roleId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject queryGrantById(@PathVariable String roleId) {
        GrantBean grantBean = this.grantService.queryGrantByRoleId(roleId.trim());
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("status", 200);
        jsonObject1.put("msg", "");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("id", grantBean==null?"":grantBean.getId());
        jsonObject2.put("grantEnterprise",grantBean== null?false:grantBean.isGrantEnterprise());
        jsonObject2.put("grantProject",grantBean== null?false:grantBean.isGrantProject());
        jsonObject2.put("grantStaff",grantBean== null?false:grantBean.isGrantStaff());
        jsonObject2.put("grantRole",grantBean== null?false:grantBean.isGrantRole());
        jsonObject2.put("grantDepartment",grantBean== null?false:grantBean.isGrantDepartment());
        jsonObject2.put("grantFeature",grantBean== null?false:grantBean.isGrantFeature());
        jsonObject2.put("grantVersion",grantBean== null?false:grantBean.isGrantVersion());
        jsonObject2.put("grantInterfaceCase",grantBean== null?false:grantBean.isGrantInterfaceCase());
        jsonObject2.put("grantInterfaceTask",grantBean== null?false:grantBean.isGrantInterfaceTask());
        jsonObject2.put("roleId", roleId);
        jsonObject1.put("data", jsonObject2);

        return jsonObject1;
    }

    /**
     * 授权
     * @param json
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject updateGrantParamByRoleId(@RequestBody JSONObject json) {
        //取值
        String id = json.getString("id");
        String roleId = json.getString("roleId");
        //加载
        GrantBean grantBean = this.grantService.queryGrantByRoleId(roleId.trim());
        //返回值
        JSONObject jsonObject = new JSONObject();
        /**
         * 如果查询出来的授权实体对象ID为空时，走保存流程，否则走更新流程
         */
        if ("".equals(id) && id.trim().length() <= 0) {
            grantBean = new GrantBean();
        }
        boolean grantEnterprise = false;
        String isGrantEnterprise = json.getString("grantEnterprise");
        if ("on".equals(isGrantEnterprise)) {
            grantEnterprise = true;
        }
        boolean grantProject = false;
        String isGrantProject = json.getString("grantProject");
        if ("on".equals(isGrantProject)) {
            grantProject = true;
        }
        boolean grantStaff = false;
        String isGrantStaff = json.getString("grantStaff");
        if ("on".equals(isGrantStaff)) {
            grantStaff = true;
        }
        boolean grantRole = false;
        String isGrantRole = json.getString("grantRole");
        if ("on".equals(isGrantRole)) {
            grantRole = true;
        }
        boolean grantDepartment = false;
        String isGrantDepartment = json.getString("grantDepartment");
        if ("on".equals(isGrantDepartment)) {
            grantDepartment = true;
        }
        boolean grantFeature = false;
        String isGrantFeature = json.getString("grantFeature");
        if ("on".equals(isGrantFeature)) {
            grantFeature = true;
        }
        boolean grantVersion = false;
        String isGrantVersion = json.getString("grantVersion");
        if ("on".equals(isGrantVersion)) {
            grantVersion = true;
        }
        boolean grantInterfaceCase = false;
        String isGrantInterfaceCase = json.getString("grantInterfaceCase");
        if ("on".equals(isGrantInterfaceCase)) {
            grantInterfaceCase = true;
        }
        boolean grantInterfaceTask = false;
        String isGrantInterfaceTask = json.getString("grantInterfaceTask");
        if ("on".equals(isGrantInterfaceTask)) {
            grantInterfaceTask = true;
        }

        //设置
        grantBean.setGrantEnterprise(grantEnterprise);
        grantBean.setGrantProject(grantProject);
        grantBean.setGrantStaff(grantStaff);
        grantBean.setGrantRole(grantRole);
        grantBean.setGrantDepartment(grantDepartment);
        grantBean.setGrantFeature(grantFeature);
        grantBean.setGrantVersion(grantVersion);
        grantBean.setGrantInterfaceCase(grantInterfaceCase);
        grantBean.setGrantInterfaceTask(grantInterfaceTask);

        if ("".equals(id) && id.trim().length() <= 0) {
            grantBean.setRoleId(roleId);
            grantBean.setGrantDate(new Date());
            this.grantService.insertGrant(grantBean);
        } else {
            grantBean.setGrantDate(new Date());
            this.grantService.updateGrantParam(grantBean);
        }

        //返回值
        jsonObject.put("status", 200);
        jsonObject.put("msg", "角色授权成功");

        return jsonObject;
    }
}
